package com.com.zs21cp.watermark;

import org.apache.flink.api.common.functions.FlatMapFunction;
import org.apache.flink.api.common.typeinfo.Types;
import org.apache.flink.api.java.ExecutionEnvironment;
import org.apache.flink.api.java.operators.AggregateOperator;
import org.apache.flink.api.java.operators.DataSource;
import org.apache.flink.api.java.operators.FlatMapOperator;
import org.apache.flink.api.java.operators.UnsortedGrouping;
import org.apache.flink.api.java.tuple.Tuple2;



public class flink_wordcount_Batch_01 {
    public static void main(String[] args) throws Exception {
        //这是批处理
        ExecutionEnvironment env = ExecutionEnvironment.getExecutionEnvironment();
        DataSource<String> textFile = env.readTextFile("F:\\java\\test_demo\\flink\\input.txt");
        FlatMapOperator<String, Tuple2<String, Integer>> flatMapOperator = textFile.flatMap((FlatMapFunction<String, Tuple2<String, Integer>>) (s, collector) -> {
            String[] split = s.split(" ");
            for (String s1 : split) {
                collector.collect(Tuple2.of(s1,1));
            }
        })
                .returns(Types.TUPLE(Types.STRING,Types.INT));
        UnsortedGrouping<Tuple2<String, Integer>> groupBy = flatMapOperator.groupBy(0);
        AggregateOperator<Tuple2<String, Integer>> sum = groupBy.sum(1);
        sum.print();

    }
}
